package com.macro.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.macro.mall.mgb.mapper.MemberReceiveAddressMapper;
import com.macro.mall.mgb.model.Member;
import com.macro.mall.mgb.model.MemberReceiveAddress;
import com.macro.mall.portal.service.MemberService;
import com.macro.mall.portal.service.UmsMemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author wangwang
 * @version 1.0
 * @description 会员收货地址管理
 * @date 2025/8/2 10:24
 */
@Service
public class UmsMemberReceiveAddressServiceImpl implements UmsMemberReceiveAddressService {
    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberReceiveAddressMapper addressMapper;

    @Override
    public int add(MemberReceiveAddress address) {
        Member currentMember = memberService.getCurrentMember();
        address.setMemberId(currentMember.getId());
        return addressMapper.insert(address);
    }

    @Override
    public int delete(Long id) {
        Member currentMember = memberService.getCurrentMember();
        return addressMapper.delete(Wrappers.<MemberReceiveAddress>lambdaQuery()
                .eq(MemberReceiveAddress::getMemberId, currentMember.getId())
                .eq(MemberReceiveAddress::getId, id));
    }

    @Override
    public int update(Long id, MemberReceiveAddress address) {
        Member currentMember = memberService.getCurrentMember();
        address.setId(null);
        if (address.getDefaultStatus() == null) {
            address.setDefaultStatus(0);
        }
        if (address.getDefaultStatus() == 1) {
            //取消原来默认地址的默认状态
            MemberReceiveAddress oldAddress = new MemberReceiveAddress();
            LambdaUpdateWrapper<MemberReceiveAddress> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(MemberReceiveAddress::getDefaultStatus, 1)
                    .eq(MemberReceiveAddress::getMemberId, currentMember.getId());
            addressMapper.update(oldAddress,wrapper);
        }
        LambdaUpdateWrapper<MemberReceiveAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MemberReceiveAddress::getMemberId, currentMember.getId())
                .eq(MemberReceiveAddress::getId, id);
        return addressMapper.update(address, wrapper);
    }

    @Override
    public List<MemberReceiveAddress> list() {
        Member currentMember = memberService.getCurrentMember();
        return addressMapper.selectList(Wrappers.<MemberReceiveAddress>lambdaQuery().eq(MemberReceiveAddress::getMemberId,currentMember.getId()));
    }

    @Override
    public MemberReceiveAddress getItem(Long id) {
        MemberReceiveAddress address = addressMapper.selectById(id);
        if(!(address == null)){
            return address;
        }
        return null;
    }
}
